#!/usr/bin/env python3
"""
为现有模型添加user_id字段
"""

import asyncio
from motor.motor_asyncio import AsyncIOMotorClient

async def fix_models():
    client = AsyncIOMotorClient('mongodb://admin:newhand@localhost:27017/')
    db = client['ai_writing']
    collection = db.model_configs

    # 为所有没有user_id的模型添加默认user_id
    result = await collection.update_many(
        {'user_id': {'$exists': False}},
        {'$set': {'user_id': 'user_123456'}}
    )

    print(f'✅ Updated {result.modified_count} models with user_id')

    # 验证
    models = await collection.find({'user_id': 'user_123456'}).to_list(length=None)
    print(f'✅ Now have {len(models)} models with user_id=user_123456')

    for model in models:
        print(f"  - {model.get('name', 'Unknown')} (enabled={model.get('enabled', False)}, status={model.get('status', 'unknown')})")

    client.close()

if __name__ == '__main__':
    asyncio.run(fix_models())